package com.ita.interceptor;

import java.util.Map;

import javax.servlet.http.HttpSession;

import org.fusesource.mqtt.codec.CONNACK;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.http.server.ServletServerHttpRequest;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.server.HandshakeInterceptor;

import com.ita.util.Constant;

public class MyHandshakeInterceptor implements HandshakeInterceptor {

	@Override
	public void afterHandshake(ServerHttpRequest arg0, ServerHttpResponse arg1, WebSocketHandler arg2, Exception arg3) {

	}

	@Override
	public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler handler,
			Map<String, Object> map) throws Exception {
		 if (request instanceof ServletServerHttpRequest) {
	            ServletServerHttpRequest serverHttpRequest = (ServletServerHttpRequest) request;
	            HttpSession session = serverHttpRequest.getServletRequest().getSession();
//	            Map parameterMap = serverHttpRequest.getServletRequest().getParameterMap();
//	            System.out.println(parameterMap);
	            if (session != null) {
	            	Object tmp =  session.getAttribute(Constant.CLIENT_ID);
	            	if(tmp != null){
	            		map.put(Constant.CLIENT_ID, tmp);
	            	}
	                
	            }

	        }
		return true;
	}

}
